var express = require('express');
var router = express.Router();
var http = require('http');
var url = require('url');


router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

//轮播的接口  http://localhost:3000/olider?time=1495538144751
router.get('/olider', function(req, res) {
	//在nodejs里我们要使用 http 模板去访问		卖座网的接口
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/billboard/home?__t=' + t;
	http.get(url, function(ress) {
		var data = '';
		ress.on('data', function(chunk) {
			data += chunk
		});
		
		ress.on('end',function(){
			res.send(data);
		})
	})
})

router.get('/reying', function(req, res) {
	//http://localhost:3000/jijiang?time=1495538144751
	//在nodejs里我们要使用 http 模板去访问		卖座网的接口
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/film/now-playing?__t=' + t+'&page=1&count=5'
	http.get(url, function(ress) {
		var data = '';
		ress.on('data', function(chunk) {
			data += chunk
		});
		
		ress.on('end',function(){
			res.send(data);
		})
	})
})

router.get('/jijiang', function(req, res) {
//	http://m.maizuo.com/v4/api/film/coming-soon?__t=1495538144760&page=1&count=3
	//在nodejs里我们要使用 http 模板去访问		卖座网的接口
	var t = req.query.time;
	var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__t=' + t+'&page=1&count=3'
	http.get(url, function(ress) {
		var data = '';
		ress.on('data', function(chunk) {
			data += chunk
		});
		
		ress.on('end',function(){
			res.send(data);
		})
	})
})

// 详情的接口    localhost:3000/3663?time=1495519120152
router.get(/\d$/, function(req, res) {
    var t = req.query.time;
//  console.log('=============');
//  console.log(req.url);
//  console.log('=============');   
    
    var id = url.parse(req.url).pathname.substr(1);
    
    var url1 = "http://m.maizuo.com/v4/api/film/" +id+ "?__t=" + t;
//  
    http.get(url1, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    });
    
})

//电影列表

router.get('/list', function(req, res) {
//	http://m.maizuo.com/v4/api/film/now-playing?page=1&count=7

	//在nodejs里我们要使用 http 模板去访问		卖座网的接口

	var url = 'http://m.maizuo.com/v4/api/film/now-playing?page=1&count=7'
	http.get(url, function(ress) {
		var data = '';
		ress.on('data', function(chunk) {
			data += chunk
		});
		
		ress.on('end',function(){
			res.send(data);
		})
	})
})
//http://m.maizuo.com/v4/api/cinema?__t=1496044931522
router.get('/cinema', function(req, res) {
	var url = 'http://m.maizuo.com/v4/api/cinema?__t=1496044931522'
	http.get(url, function(ress) {
		var data = '';
		ress.on('data', function(chunk) {
			data += chunk
		});
		
		ress.on('end',function(){
			res.send(data);
		})
	})
})




/* GET home page. */
router.get('/', function(req, res, next) {
	res.render('index', {
		title: 'Express'
	});
});

module.exports = router;